<?php
// +----------------------------------------------------------------------
// | 优惠卷管理
// +----------------------------------------------------------------------
namespace app\app\controller\v2;
use app\app\Appbase;

class Coupon extends Appbase 
{
	
	/**
	* 获取优惠券列表
	* /app/v2/coupon/getList
	* 需要登录 
	* @is_need_login true
	*/
	public function getList($name='',$pageIndex=1,$pageSize=20){
		try {
			$where[] = [ 'shop_id', '=', request()->shopId ];
			$where[] = [ 'coupon_name', 'like', "%$name%"];
			$result = model('Coupon')->getCouponList($where,$pageIndex,$pageSize);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取优惠券核销列表
	* /app/v2/coupon/add
	* 需要登录 
	* @is_need_login true
	*/
	public function add(){
		$request =  request();
		$param = $request ->post();
		$param['shop_id'] = $request ->shopInfo['id'];
		$param['remain_count'] = $param['total_count'];
		try {
			$result = model('Coupon')->add($param);
			if($result === false){
				return $this->ReError( model('Coupon')->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*移除
	* /app/v2/coupon/del
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function del($id){
		if( !trim($id) ){
			return $this->ReError('未找到对应id参数!');
		}
		try {
			$db = model('Coupon');
			$result = $db->remove($id);
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
}

